package com.example.mq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: ConfirmConfig
 * author: bob.ly
 * Version: 1.0.0
 * DateTime: 2021/07/26-22:12:00
 * Description:
 * 凡是修改了配置文件,都得把队列或者交换机删除掉
 */
@Configuration
public class ConfirmConfig {
    private static final String CONFIRM_EXCHANGE_NAME = "confirm.exchange";
    private static final String CONFIRM_QUEUE_NAME = "confirm.queue";

    /**
     * 以下为demo3的备胎交换机和队列成员变量
     */
    private static final String BACK_UP_EXCHANGE_NAME = "backup.exchange";
    private static final String BACK_UP_QUEUE = "backup.queue";
    private static final String WARNING_QUEUE = "warning.queue";

    @Bean("confirmExchange")
    public DirectExchange confirmExchange() {
        ExchangeBuilder
                exchangeBuilder =
                ExchangeBuilder.directExchange(CONFIRM_EXCHANGE_NAME)
                        .durable(true)
                        //设置该交换机的备份交换机
                        .withArgument("alternate-exchange", BACK_UP_EXCHANGE_NAME);
        return (DirectExchange) exchangeBuilder.build();
    }

    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(CONFIRM_QUEUE_NAME).build();
    }

    @Bean
    public Binding queueBindingExchange(@Qualifier("confirmExchange") DirectExchange directExchange
            , @Qualifier("confirmQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("key1");
    }

    /**
     * 以下为demo3的备胎交换机和队列,以及绑定
     */
    @Bean("backUpExchange")
    public FanoutExchange backUpExchange() {
        return new FanoutExchange(BACK_UP_EXCHANGE_NAME);
    }

    @Bean("backupQueue")
    public Queue backupQueue() {
        return QueueBuilder.durable(BACK_UP_QUEUE).build();
    }

    @Bean("warningQueue")
    public Queue warningQueue() {
        return QueueBuilder.durable(WARNING_QUEUE).build();
    }

    @Bean
    public Binding warningBinding(@Qualifier("warningQueue") Queue queue,
                                  @Qualifier("backUpExchange") FanoutExchange
                                          backupExchange) {
        return BindingBuilder.bind(queue).to(backupExchange);
    }

    @Bean
    public Binding backupBinding(@Qualifier("backupQueue") Queue queue,
                                 @Qualifier("backUpExchange") FanoutExchange backupExchange) {
        return BindingBuilder.bind(queue).to(backupExchange);
    }
}
